package kr.saeildamil.androidstudy.ch16;

import java.util.Calendar;

import kr.saeildamil.androiddforstudy.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class AlertDlg extends Activity {
	
	final static int SAMPLEDLG = 0;
	final static int QUESTIONDLG = 1;
	int a = 3;
	int b = 4;
	int result;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch16_alertdialog);
	}
	
	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.ch16_alertdialog_dialogbtn:
			AlertDialog.Builder bld01 = new AlertDialog.Builder(this);
			bld01.setTitle("Notice");
			bld01.setMessage("Dialog was opened.");
			bld01.setIcon(R.drawable.ic_launcher);
			bld01.show();
			
			break;
		case R.id.ch16_alertdialog_dialogbtn01:
			new AlertDialog.Builder(this)
			.setTitle("Notice")
			.setMessage("Dialog was opened.")
			.setIcon(R.drawable.ic_launcher)
			.setNegativeButton("Close", null)
			.show();
			break;
		case R.id.ch16_alertdialog_dialogbtn02:
			new AlertDialog.Builder(this)
			.setTitle("Notice")
			.setMessage("Dialog was opened.")
			.setIcon(R.drawable.ic_launcher)
			.setNegativeButton("Close", null)
			.setPositiveButton("OK", null)
			.show();
			break;
		case R.id.ch16_alertdialog_dialogbtn03:
			new AlertDialog.Builder(this)
			.setTitle("Notice")
			.setMessage("Dialog was opened.")
			.setIcon(R.drawable.ic_launcher)
			.setNegativeButton("Close", null)
			.setPositiveButton("OK", null)
			.setNeutralButton("Apply", null)
			.show();
			break;
		case R.id.ch16_alertdialog_dialogbtn04:
			showDialog(SAMPLEDLG);
			break;
		case R.id.ch16_alertdialog_dialogbtn05:
			showDialog(QUESTIONDLG);
			break;
		case R.id.ch16_alertdialog_dialogbtn06:
			showModal();
			break;
		case R.id.ch16_alertdialog_dialogbtn07:
			new AlertDialog.Builder(this)
			.setTitle("Question")
			.setMessage("Which one?")
			.setNegativeButton("*", listener)
			.setPositiveButton("+", listener)
			.show();
			break;
		}
	}//end of method mOnClick;

	DialogInterface.OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			if(which==DialogInterface.BUTTON_POSITIVE){
				result=a+b;
			}else{
				result=a*b;
			}
			TextView tv = (TextView) findViewById(R.id.ch16_alertdialog_resulttv);
			tv.setText("Answer is : " + result);
			Toast.makeText(AlertDlg.this, "Completed", Toast.LENGTH_SHORT).show();
		}
	};
	
	private void showModal() {
		try{
			Thread.sleep(3000);
		}catch(Exception e){}
		boolean stat = true; 	// Exception Occurred!
		if(stat){
			new AlertDialog.Builder(this)
			.setTitle("Exception Occurred!")
			.setMessage("Exception occurred because of this!")
			.setPositiveButton("Close", new OnClickListener() {
	
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			})
			.show();
			
			// finish(); // Activity 가 종료됨;
		}
		
		Toast.makeText(this, "Finished successfully!", Toast.LENGTH_SHORT).show();
	}

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		if(id==SAMPLEDLG){
			return new AlertDialog.Builder(this)
			.setTitle("Notice")
			.setMessage("Dialog was opened.")
			.setIcon(R.drawable.ic_launcher)
			.setPositiveButton("Close", null)
			.setCancelable(false)
			.create();
		}else{
			return new AlertDialog.Builder(this)
			.setTitle("Question")
			.setMessage("Did you have a lunch?")
			.setPositiveButton("Yes", null)
			.setNegativeButton("No", null)
			.create();
		}
	}

	@Override
	@Deprecated
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		switch (id) {
		case SAMPLEDLG:
			break;
		case QUESTIONDLG:
			Calendar cal = Calendar.getInstance();
			String sTime = String.format("%d시 %d분 %d초", cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
			dialog.setTitle(sTime);
			break;
		}
	}
}	
